package com.atguigu.springcloud0419.controller;

import com.atguigu.springcloud0419.entities.CommonResult;
import com.atguigu.springcloud0419.entities.Payment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * 服务消费者模块
 */
@RestController
public class OrderController {

//public static final String PaymentSrv_URL = "http://localhost:8001"; 此处不能把端口号写死了

    // 通过在eureka上注册过的微服务名称调用
    public static final String PaymentSrv_URL = "http://CLOUD-PAYMENT-SERVICE";
@Autowired
public RestTemplate restTemplate; //将已经装配进ioc容器中的restTemplate在服务层调用，
// 用于在其方法中传入服务提供者端口号，调用provider层


    @GetMapping("/consumer/payment/create") //客户端用浏览器是get请求，但是底层实质发送post调用服务端8001
    public CommonResult create(Payment payment)
    {
        return restTemplate.postForObject(PaymentSrv_URL + "/payment/create",payment,CommonResult.class);
    }


    @GetMapping("/consumer/payment/get/{id}")
    public CommonResult getPayment(@PathVariable Long id)
    {
        return restTemplate.getForObject(PaymentSrv_URL + "/payment/get/"+id, CommonResult.class, id);
    }

}
